package com.jasic.test.socketServer;

import com.jasic.common.GlobalConstant;
import com.jasic.comnt.protocol.bean.BasePacket;
import com.jasic.comnt.protocol.bean.LoginResp;
import com.jasic.comnt.protocol.bean.PostionResp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * User: Jasic
 * Date: 13-1-4
 */
public class SimpleSocketServer {


    private ServerSocket server;

    private int port;

    private Map<Socket, SocketThread> socketThreadMap;

    public SimpleSocketServer(int port) {
        this.port = port;
        try {
            this.server = new ServerSocket(port);
            this.socketThreadMap = new ConcurrentHashMap<Socket, SocketThread>();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void recv() {
        try {
            while (true) {
                Socket socket = this.server.accept();
                SocketThread thread = this.socketThreadMap.get(socket);
                if (null == thread) {
                    thread = new SocketThread(socket);
                    socketThreadMap.put(socket, thread);
                    thread.start();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    class SocketThread extends Thread {

        Socket socket;

        SocketThread(Socket socket) {
            this.socket = socket;
        }


        @Override
        public void run() {

            recv();

        }


        private void recv() {
            int index = 0;
            try {
                InputStream sis = this.socket.getInputStream();
                byte[] b = new byte[1024];
                while (true) {
                    index++;
                    int len = sis.read(b);
                    if (len == -1) {
                        System.out.println("read code:-1, return!");
                        return;
                    }

                    System.out.print("Receive loginresp....: ");
                    for (int i = 0; i < len; i++) {
                        System.out.print(b[i] + " ");
                    }

                    System.out.print("Sending loginresp....");

                    OutputStream sos = this.socket.getOutputStream();

                    byte[] bys = new byte[]{0x0};

                    BasePacket resp = null;
                    if (len == GlobalConstant.LOGIN_LENGTH) {
                        System.out.println("发送....登录响应");
                         resp = new LoginResp(0);
                    } else {
                         resp = new PostionResp(0, "15989133047", 1, 2, 0, 22, 33, 11, 299, 99);
                        System.out.println("发送....位置响应");
                    }

                    bys = resp.toByte();

                    for (byte bs : bys) {
                        System.out.print(bs + " ");
                    }

                    System.out.println();

                    sos.write(bys);
                    sos.flush();

                    System.out.println("接收与发送与成功");


                    System.out.print(index);
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.print(index);
            }
        }
    }

    public static void main(String[] args) {
        new SimpleSocketServer(4444).recv();
    }
}
